package com.oracle.rentcar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oracle.rentcar.mapper.CustomerMapper;
import com.oracle.rentcar.pojo.Customer;
import com.oracle.rentcar.service.CustomerService;
import com.oracle.rentcar.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    //业务层调用数据访问层
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> selectCustomer() {
        return customerMapper.selectCustomer();
    }

    @Override
    public void delete(int id) {
        customerMapper.deleteCustomer(id);
    }

    @Override
    public int removeCustomers(Integer[] ids) {
        // Arrays.asList(ids) 数组转成list集合
        int n = customerMapper.bathDeleteCustomer(Arrays.asList(ids));
        return n;
    }


    @Override
    public long count() {
        return customerMapper.countCustomer();
    }

    @Override
    public Customer getCustomerById(Integer id) {
        return customerMapper.findCustomer(id);
    }

    @Override
    public void updateCustomerById(Customer customer) {
        customerMapper.updateCustomerById(customer);
    }

    @Override
    public PageInfo<Map<String, Object>> findCustomerByPage(int currentPage, String cusName, String phone) {

        PageHelper.startPage(currentPage, Constant.PAGE_SIZE);

        List<Map<String, Object>> list = customerMapper.selectCustomerByParams(cusName, phone);

        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public boolean checkCustomerNameExist(String cusName) {

        Customer customer = customerMapper.checkCustomerName(cusName);

        if (customer != null) {   // 有这个会员名，不能使用这个会员名
            return false;
        }

        return true;   // 没有这个会员名，可以使用这个会员名
    }

    @Override
    public int reg(Customer customer) {
        return customerMapper.addCustomer(customer);
    }
}
